//
//  GoogleToBaidu.m
//  WildDonkey
//
//  Created by 孙俊 on 15/3/30.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "GoogleToBaidu.h"
#import "AFNetworking.h"
@interface GoogleToBaidu()

@property (nonatomic,retain) NSData *data;

@end



@implementation GoogleToBaidu

//将 ios 系统获得的经纬度转换为百度坐标系 <注意返回的经纬度,需结合当地经纬校验一次>
- (instancetype)initLongitude:(float)longitude latitude:(float)latitude
{
	self = [super init];
	if (self) {
	
		NSString *webAK = @"220ktXLZVlXd0kGpaxLzhvHa";
		
		NSString *lon = [NSString stringWithFormat:@"%.8f",longitude];//经度
		NSString *lat = [NSString stringWithFormat:@"%.8f",latitude];//纬度
		
		NSString *urlstr = [NSString stringWithFormat:@"http://api.map.baidu.com/geoconv/v1/?coords=%@,%@&from=3&to=5&ak=%@",lon,lat,webAK];
		
		//NSURL *url = [NSURL URLWithString:urlstr];
		
//		NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:5];
//		
//		_data = [[NSData alloc] init];
//		
//
//		self.data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//		
//		
//		
//		NSDictionary *dic1 = [NSJSONSerialization JSONObjectWithData:self.data options:(NSJSONReadingMutableLeaves) error:nil];
		
		AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
		[manager GET:urlstr parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
			NSLog(@"[GoogelToBaidu networking] %@",responseObject);

			
			NSArray *array1 = [responseObject valueForKey:@"result"];
			
			NSDictionary *dic2 = [array1 lastObject];
			
			self.x = [[dic2 valueForKey:@"y"] floatValue];
			self.y = [[dic2 valueForKey:@"x"] floatValue];
	
		} failure:^(NSURLSessionDataTask *task, NSError *error) {
			
		}];
		


//		
//		[_data release];
//		_data = nil;
//		[request release];
//		request = nil;
	}
	return self;
}

//转换的类方法 <注意返回的经纬度,需结合当地经纬校验一次>
+ (GoogleToBaidu *)GoogleToBaiduLongitude:(float)longitude latitude:(float)latitude
{
	return [[[GoogleToBaidu alloc] initLongitude:longitude latitude:latitude] autorelease];
}


@end
